package cn.fl.capital.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapFileInfoQueryCondition;
import cn.fl.capital.constant.CapFileInfoConstant;
import cn.fl.capital.service.ICapFileInfoService;
import cn.fl.capital.vo.CapFileInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysUlFilesRecService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/capital/capFundFile")
public class CapFileInfoController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(CapFileInfoController.class);

    @Autowired
    private ICapFileInfoService capFileInfoService;

    @Autowired
    private ISysUlFilesRecService sysUlFilesRecService;

    /**
     * 初始化转来款上传资料列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/initTransIncomeFileList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initTransIncomeFileList(HttpServletRequest request, HttpServletResponse response) {
        ListPageVO<CapFileInfoVO> listPageVO = new ListPageVO<>();
        try {
            String outApplyId = request.getParameter("outApplyId");
            BaseLogUtils.info(logger, "initTransIncomeFileList", "初始化转来款上传资料列表.", CurrentThreadContext.getCurrentUserName());
            CapFileInfoQueryCondition condition = new CapFileInfoQueryCondition();
            condition.setDataId(Long.parseLong(outApplyId));
            condition.setCfgCd(CapFileInfoConstant.CODE_UL_CAP_FILE_CFG_CD);

            List<CapFileInfoVO> list = capFileInfoService.searchCapFundFileInfoList(condition);
            if (CollectionUtils.isNotEmpty(list)) {
                listPageVO.setRows(list);
                listPageVO.setTotal(list.size());
            } else {
                listPageVO.setRows(new ArrayList<CapFileInfoVO>());
                listPageVO.setTotal(list.size());
            }
        } catch (BaseException e) {
            BaseLogUtils.error(logger, "initTransIncomeFileList", "初始化转来款上传资料列表数据异常", e);
        }
        return listPageVO;
    }

    /**
     * 上传转来款资料
     * @param request
     * @param response
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/uploadCapTransFile.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object uploadCapTransFile(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_FILE_UPLOAD_SUCC);
        String fileParams = request.getParameter("fileParams");
        String cfgCd = request.getParameter("cfgCd");
        String cfgDtlCd = request.getParameter("cfgDtlCd");
        String orgCd = request.getParameter("orgCd");
        String dataId = request.getParameter("dataId");
        if (StringUtils.isBlank(dataId)) {
            BaseLogUtils.error(logger, "uploadCapTransFile", "上传转来款资料文件失败,主键项目ID为空==>cfgCd=" + cfgCd + ",cfgDtlCd=" + cfgDtlCd);
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_FILE_UPLOAD_FAIL);
        } else {
            boolean flag = this.sysUlFilesRecService.addAttchmnt(dataId, fileParams, cfgCd, cfgDtlCd, "CAP_FUND_OUT_APPLY", null, null, null, false, orgCd);
            if (!flag) {
                BaseLogUtils.error(logger, "uploadCapTransFile", "上传转来款资料文件失败==>prjId=" + dataId + ",cfgCd=" + cfgCd + ",cfgDtlCd=" + cfgDtlCd + ",file=" + fileParams);
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_FILE_UPLOAD_FAIL);
                return result;
            }
            //result.setData(this.capPayInfoService.getPayMateCnt(Long.valueOf(prjPrdDtlId)));
            BaseLogUtils.info(logger, "uploadCapTransFile", "上传转来款资料文件成功==>prjId=" + dataId + ",cfgCd=" + cfgCd + ",cfgDtlCd=" + cfgDtlCd);
        }
        return result;
    }

    /**
     * 图片展示
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/imgWinOpen.do", method = { RequestMethod.GET,  RequestMethod.POST })
    public ModelAndView imgWinOpen(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("project/imgWinOpen");
        return view;
    }

    /**
     * 删除资料
     * @param request
     * @param response
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/removeFile.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removeFile(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        String fileId = request.getParameter("fileId");
        if (StringUtils.isEmpty(fileId)) {
            BaseLogUtils.info(logger, "removeFile", "删除资金管理资料==>资料ID为空");
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
            return result;
        }

        boolean success = this.capFileInfoService.removeFile(Long.valueOf(fileId));
        BaseLogUtils.info(logger, "", "删除资金管理资料,id=" + fileId + ",success=" + success, CurrentThreadContext.getCurrentUserName());
        if (!success) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
        }
        return result;
    }
}
